package com.ssd.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ssd.entity.PartnerEntity;
import com.ssd.entity.vo.PartnerVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface PartnerDao extends BaseMapper<PartnerEntity> {

    @Select("<script>" +
            "SELECT p.* ," +
            "COUNT(n.id) AS node_count " +
            "FROM tb_partner p " +
            "LEFT JOIN tb_node n ON p.id = n.partner_id " +
            "<where> " +
            "<if test='partnerName != null and partnerName != \"\"'> " +
            "AND p.partner_name like CONCAT('%', #{partnerName}, '%') " +
            "</if> " +
            "</where> " +
            "GROUP BY p.id" +
            "</script>")
    @Results({
            @Result(property="id", column="id"),
            @Result(property="partnerName", column="partner_name"),
            @Result(property="contactPerson", column="contact_person"),
            @Result(property="contactPhone", column="contact_phone"),
            @Result(property="profitRatio", column="profit_ratio"),
            @Result(property="account", column="account"),
            @Result(property="password", column="password"),
            @Result(property="createTime", column="create_time"),
            @Result(property="updateTime", column="update_time"),
            @Result(property="createBy", column="create_by"),
            @Result(property="updateBy", column="update_by"),
            @Result(property="remark", column="remark"),
            @Result(property="nodeCount", column="node_count")
    })
    Page<PartnerVo> selectPartnerList(Page<?> page,String partnerName);

    @Select("<script>" +
            "SELECT p.* ," +
            "COUNT(n.id) AS node_count " +
            "FROM tb_partner p " +
            "LEFT JOIN tb_node n ON p.id = n.partner_id " +
            "WHERE p.id = #{partnerId} " +
            "</script>")
    @Results({
            @Result(property="id", column="id"),
            @Result(property="partnerName", column="partner_name"),
            @Result(property="contactPerson", column="contact_person"),
            @Result(property="contactPhone", column="contact_phone"),
            @Result(property="profitRatio", column="profit_ratio"),
            @Result(property="account", column="account"),
            @Result(property="password", column="password"),
            @Result(property="createTime", column="create_time"),
            @Result(property="updateTime", column="update_time"),
            @Result(property="createBy", column="create_by"),
            @Result(property="updateBy", column="update_by"),
            @Result(property="remark", column="remark"),
            @Result(property="nodeCount", column="node_count")
    })
    PartnerVo selectPartnerById(Long partnerId);
}